package com.shiguo365.market.display.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.shiguo365.market.R;
import com.shiguo365.market.display.base.BaseActivity;
import com.shiguo365.market.display.util.ActivityCollector;
import com.shiguo365.market.display.view.ILoginView;
import com.shiguo365.market.presenter.implement.LoginPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements ILoginView {
    LoginPresenter loginPresenter;

    @BindView(R.id.buttonLogin) Button btn_login; //首页按钮
    @BindView(R.id.editTextusername) EditText edt_username;
    @BindView(R.id.editTextpassword) EditText edt_password;
    @BindView(R.id.progressBarlogin) ProgressBar pbar_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this); //ButterKnife初始化
        loginPresenter =new LoginPresenter(this,this);
        pbar_login.bringToFront();
        pbar_login.setVisibility(View.GONE);


    }

    @OnClick(R.id.buttonLogin)
    public void login() {


        String username=edt_username.getText().toString();
        String password=edt_password.getText().toString();
        if("".equals(username) || "".equals(password)){
            this.ShowMessages("用户名/密码不能为空");
            return;
        }
        pbar_login.setVisibility(View.VISIBLE);
        loginPresenter.login(username,password);

    }

    @OnClick(R.id.buttonReg)
    public void reg(){
        Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
        startActivity(intent);

    }

    @OnClick(R.id.buttonForget)
    public void forget(){
        Intent intent = new Intent(LoginActivity.this,ForgetActivity.class);
        startActivity(intent);

    }

    //显示消息
    @Override
    public void ShowMessages(String msg) {
        Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
        pbar_login.setVisibility(View.GONE);
    }

    //登录成功进入首页
    @Override
    public void LoginSuccess() {
        pbar_login.setVisibility(View.GONE);
        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
        startActivity(intent);
        finish();
    }
}
